<template>
  <el-dialog :title="formData.id?'编辑房产': '添加房产'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :model="formData" label-width="120px" :rules="rules">
      <el-form-item label="经纪">
        <el-select v-model="formData.category" placeholder="请选择经纪人">
          <el-option v-for="item in categoriesList" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="formData.agent" placeholder="请选择分类">
          <el-option v-for="item in agentsList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="标签">
        <el-tag
          v-for="tag in formData.tags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="formData.address" />
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="formData.price" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" :rows="4" resize="none" />
      </el-form-item>
      <el-form-item label="内容">
        <!-- 富文本 -->
        <quillEditor v-model="formData.content" />
      </el-form-item>
      <el-form-item label="介绍图片">
        <UpImg ref="ProImg" />
      </el-form-item>
      <el-form-item label="封面">
        <UpImg ref="UpImg" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// 富文本
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import UpImg from '@/components/UpImg/index.vue'
import { addPropety, getPropetyId, updatePropety } from '@/api/propety'
export default {
  components: { UpImg, quillEditor },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    categoriesList: {
      type: Array,
      required: true
    },
    agentsList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      formData: {
        title: '', // 标题
        desc: '', // 描述
        image: '', // 封面
        agent: '', // 经纪
        content: '', // 内容
        address: '', // 地址
        price: '', // 价格
        photos: '', // 介绍
        tags: [], // 标签
        category: '' // 分类
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {

    // 获取对应id房产的数据
    async getPropetyId(id) {
      const res = await getPropetyId(id)
      console.log('id', res)
      this.formData = res.data
      if (res.data.category) {
        this.formData.category = res.data.category.id
      } else {
        this.formData.category = ''
      }
      if (res.data.agent) {
        this.formData.agent = res.data.agent.id
      } else {
        this.formData.agent = ''
      }
      this.$refs.UpImg.dialogImageUrl = this.formData.image
      this.$refs.ProImg.dialogImageUrl = this.formData.photos
      // console.log(this.formData.id)
    },
    // 提交
    async submit() {
      // 表单校验
      await this.$refs.form.validate()
      this.formData.image = this.$refs.UpImg.dialogImageUrl || this.$refs.UpImg.imgUrl // 封面
      this.formData.photos = this.$refs.ProImg.dialogImageUrl || this.$refs.ProImg.imgUrl // 介绍
      if (this.formData.id) {
        await updatePropety(this.formData)
      } else {
        // 调用接口
        await addPropety(this.formData)
      }
      // 刷新页面
      this.$emit('updateEvent')
      // 关闭弹窗
      this.closeFn()
    },
    // 回显图片
    EchoImg() {
      this.$refs.UpImg.dialogImageUrl = this.formData.image
      this.$refs.UpImg.sss = 1
    },
    // 关闭
    closeFn() {
      // 清空数据
      this.formData = {
        title: '', // 标题
        desc: '', // 描述
        image: '', // 图片
        agent: '', // 经纪
        content: '', // 内容
        address: '', // 地址
        price: '', // 价格
        photos: '', // 图片
        tags: [], // 标签
        category: '' // 分类
      }
      this.$refs.UpImg.imgUrl = ''
      this.$refs.UpImg.handleRemove()
      this.$refs.ProImg.imgUrl = ''
      this.$refs.ProImg.handleRemove()
      this.$refs.form.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    // 标签插件用的
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }

  }
}
</script>

<style scoped lang= 'scss'>
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
